package com.example.domain.result;

public class Result<T> {

    private boolean isSuccess;
    private T data;
    private Throwable throwable;
    private String errorMsg;

    Result(boolean isSuccess, T data, Throwable throwable, String errorMsg) {
        this.isSuccess = isSuccess;
        this.data = data;
        this.throwable = throwable;
        this.errorMsg = errorMsg;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, data, null, null);
    }

    public static Result error(Throwable throwable, String errorMsg) {
        return new Result(false, null, throwable, errorMsg);
    }

    public T getDataWhenSuccess() {
        if (!isSuccess) {
            return null;
        }
        return data;
    }

    public String getErrorMsgWhenError() {
        if (isSuccess) {
            return null;
        }
        return errorMsg;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
